package com.mars.attach.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.mars.attach.common.utils.file.FileMeta;
import com.mars.attach.common.utils.file.FileUtils;
import com.mars.attach.common.utils.file.FileUtils.Thumbnail;
import com.mars.base.common.utils.web.WebUtil;
import com.mars.base.web.BaseController;

@RestController
public class UploadController extends BaseController {
	
	/**
	 * 同域名下上传文件
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/local/upload")
	public Object upload(HttpServletRequest request, HttpServletResponse response) {
		//用户指定上传目录
		String destDir = request.getParameter("destDir") ;
		//是否生成缩略图
		String thumbnail = request.getParameter("thumbnail") ;
		
		Map<String, Object> map = new HashMap<String, Object>() ;
		List<FileMeta> fileMetas = FileUtils.upload(destDir, (null != thumbnail && StringUtils.isNotBlank(thumbnail) ? Thumbnail.yes : Thumbnail.no), request) ;
		map.put("files", fileMetas) ;
		
		logger.debug("上传文件：{}", JSON.toJSONString(map));
		return map ;
	}
	
	/**
	 * jQuery File Upload 跨域文件上传
	 * <br>需要在JS中指定如下属性：
	 * <br>forceIframeTransport: true,
	 * <br>redirectParamName:"cors_result_url",
	 * <br>redirect:"http://"+window.location.host+"/pages/other/upload/cors/result.html?",//结果集回调页面
	 * WebUploader 跨域文件上传，或其他
	 * @param request
	 * @param response
	 */
	@PostMapping(value = "/cors/upload")
	public Object Object(HttpServletRequest request, HttpServletResponse response) {
		//jQuery File Upload 跨域上传文件的返回值需要通过重定向完成，跨域返回结果集，需要重定向到指定URL，并携带结果集
		String cors_result_url = request.getParameter("cors_result_url") ;
		//用户指定上传目录
		String destDir = request.getParameter("destDir") ;
		//是否生成缩略图
		String thumbnail = request.getParameter("thumbnail") ;
		
		Map<String, Object> map = new HashMap<String, Object>() ;
		
		List<FileMeta> fileMetas = FileUtils.upload(destDir, (null != thumbnail && StringUtils.isNotBlank(thumbnail) ? Thumbnail.yes : Thumbnail.no), request) ;
		map.put("files", fileMetas) ;
		
		logger.debug("上传文件：{}", JSON.toJSONString(map));
		
		//jQuery File Upload 方式，返回结果集
		if(null != cors_result_url && !"".equals(cors_result_url.trim())) {
			try {
				response.sendRedirect(cors_result_url + WebUtil.encode(JSON.toJSONString(map)));
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {//其他，如：WebUploader
			return map ;
		}
		
		return null ;
	}
	
	/*@PostMapping(value = "/webuploader/cors/upload")
	public Object webUploaderCorsUpload(HttpServletRequest request, HttpServletResponse response) {
		//用户指定上传目录
		String destDir = request.getParameter("destDir") ;
		//是否生成缩略图
		String thumbnail = request.getParameter("thumbnail") ;
		
		Map<String, Object> map = new HashMap<String, Object>() ;
		
		List<FileMeta> fileMetas = FileUtils.upload(destDir, (null != thumbnail && StringUtils.isNotBlank(thumbnail) ? Thumbnail.yes : Thumbnail.no), request) ;
		map.put("files", fileMetas) ;
		
		logger.debug("上传文件：{}", JSON.toJSONString(map));
		
		return map ;
	}*/
	
}
